package com.xinruke.hostels.manage.user.controller.pms;

import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.controller.BaseController;
import com.xinruke.hostels.common.po.YewuyonghuCaozuoRizhi;
import com.xinruke.hostels.common.service.OperateLogService;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.manage.user.interfaces.dto.*;
import com.xinruke.hostels.manage.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author GuoZheng
 * @ClassName UserInfoPMSController
 * @date：2019/7/16
 * @version: 1.0
 * @description：pms用户管理
 */
@RestController
@RequestMapping("user/info/pms")
@Api(tags = "PMS用户管理")
@Slf4j
public class UserInfoPMSController extends BaseController {

    @Resource
    private UserService userService;
    @Autowired
    OperateLogService operateLogService;

    @Resource
    ManageUserCache manageUserCache;

    @PostMapping(value = "add")
    @ApiOperation(value = "添加PMS用户信息")
    public ResultVO addPmsUserInfo(@Valid @RequestBody UserInfoPMSAddDTO userInfoAddDTO) {
        ResultVO resultVO = userService.addPmsUserInfo(userInfoAddDTO);
        if (resultVO.getCode() == ResultVO.SUCCESS) {
            YewuyonghuCaozuoRizhi caozuoRizhi = new YewuyonghuCaozuoRizhi("用户列表", "添加PMS用户信息", "用户名[" + userInfoAddDTO.getYonghuming() + "]");
            int i = operateLogService.writeOperateLog(caozuoRizhi);
            if (i < 1) {
                log.info("写入操作日志失败");
            }
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
            ;
        }
        return resultVO;
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "查询PMS用户列表")
    public ResultVO<RowsDataVO<UserInfoQueryResultDTO>> getUserInfoList(@Valid @RequestBody UserInfoQueryDTO userInfoQueryDTO) {
        ResultVO<RowsDataVO<UserInfoQueryResultDTO>> resultVO = ResultVO.fail();
        userInfoQueryDTO.setJiudianId(manageUserCache.getJiudianId());
        RowsDataVO<UserInfoQueryResultDTO> data = userService.getUserInfoList(userInfoQueryDTO);
        resultVO.setCode(ResultVO.SUCCESS);
        resultVO.setMessage("操作成功");
        resultVO.setData(data);

        return resultVO;
    }


    @PostMapping(value = "/role")
    @ApiOperation(value = "查询当前登录用户的角色")
    public ResultVO<List<UserRolesQueryResultDto>> getUserRolesList(@Valid @RequestBody UserRolesPMSQueryDto userInfoDetailDto) {
        ResultVO<List<UserRolesQueryResultDto>> resultVO = ResultVO.fail();
        UserRolesQueryDto queryDto = new UserRolesQueryDto();
        queryDto.setUserId(manageUserCache.getCurrentUserId());
        List<UserRolesQueryResultDto> data = userService.getUserRolesList(queryDto);
        resultVO.setCode(ResultVO.SUCCESS);
        resultVO.setMessage("操作成功");
        resultVO.setData(data);

        return resultVO;
    }

}
